<template>
  <!-- 搜索工作栏 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="48px"
    >
      <el-form-item label="角色" prop="cosplayId">
        <el-select @change="handleQuery" v-model="queryParams.roleId"
                   class="!w-240px">
          <el-option
            v-for="item in cosplayOptionList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
<!--        <el-button @click="handleQuery">-->
<!--          <Icon class="mr-5px" icon="ep:search" />-->
<!--          搜索-->
<!--        </el-button>-->
<!--        <el-button @click="resetQuery">-->
<!--          <Icon class="mr-5px" icon="ep:refresh" />-->
<!--          重置-->
<!--        </el-button>-->
        <el-button
          v-hasPermi="['lm:story:create']"
          plain
          type="primary"
          @click="openForm('create')"
        >
          <Icon class="mr-5px" icon="ep:plus" />
          新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="isLoading" :data="dataList" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="场景ID" align="center" prop="id" />
      <el-table-column label="场景名称" align="center" prop="name" />
      <el-table-column label="角色扮演" align="center" prop="rpg" />
      <el-table-column label="心跳指数" align="center" prop="heartbeatLevel" />
      <el-table-column label="时间长度（秒）" align="center" prop="duration" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['lm:story:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['lm:story:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <StoryForm ref="formRef" @success="handleQuery" />
  <SimpleAudioPlayer ref="simpleAudioPlayerRef" />
</template>
<script setup lang="ts" name="CosplayStory">
import * as StoryApi from '@/api/lm/story'
import StoryForm from './StoryForm.vue'
import { CosplayApi } from '@/api/lm/cosplay'
import {useAppStore} from "@/store/modules/app";
import AudioBatchForm from "@/views/audio/list/AudioBatchForm.vue";
import SimpleAudioPlayer from "@/views/audio/components/SimpleAudioPlayer.vue";

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const appStore = useAppStore()
const isLoading = ref(false) //是否加载中
const cosplayOptionList = ref([]);
const dataList = ref([]);
const queryParams = ref({
  roleId: "",
  pageNo: 1,
  pageSize: 100
});
// 获得表格的各种操

/** 添加/修改操作 */
const simpleAudioPlayerRef = ref()
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open({
    type,
    id,
    roleId: queryParams.value.roleId,
    roleList: cosplayOptionList.value,
    role: queryParams.value.roleId
  })
}

const handleQuery = ()=> {
  isLoading.value = true;
  StoryApi.getStoryPage({
    ...queryParams.value
  }).then(e=> {
    dataList.value = e.list;
  }).finally(()=> {
    isLoading.value = false;
  });
}


/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await StoryApi.deleteStory(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    handleQuery()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  // getList()

  CosplayApi.getCosplayPage({

  }).then(e=> {
    cosplayOptionList.value = e.list.map(e=> {
      return {
        label: e.name,
        value: e.id
      }
    });
    queryParams.value.roleId = e.list[0].id;

    handleQuery();
  });

  appStore.setSimpleAudioPlayer(simpleAudioPlayerRef.value);
})
</script>
